package loginuser

import (
	"gitee.com/tomatomeatman/golang-repository/bricks3/model/dbinfo"
	"gitee.com/tomatomeatman/golang-repository/bricks3/model/globalvariable"
	"gitee.com/tomatomeatman/golang-repository/bricks3/utils/app/moduleutil"
	"gitee.com/tomatomeatman/golang-repository/bricks3/utils/ginutil"
	"gitee.com/tomatomeatman/golang-repository/bricks3/utils/sqlfactory"
	"github.com/gin-gonic/gin"
)

/**
 * 初始化
 */
func init() {
	loginUserController := &LoginUserController{}
	globalvariable.Add("Module@LoginUserController", loginUserController)
	loginUserController.RegisterUrl()
}

// @Controller 用户表控制器
type LoginUserController struct {
}

// -- 检查待新增内容是否存在重复数据(单独字段重复即重复)集合 --//
func (control LoginUserController) CheckRepeatAlone() []string {
	return []string{"sName", "sNo"}
}

// 接口注册
func (control LoginUserController) RegisterUrl() {
	ginutil.AddUrl("/login/user/add", control.Add, ginutil.POST)
	ginutil.AddUrl("/login/user/password/edit", control.EditPass, ginutil.POST)
	ginutil.AddUrl("/login/user/password/reset", control.ResetPass, ginutil.POST)
	ginutil.AddUrl("/login/user/edit/type", control.EditUserType, ginutil.POST)
	ginutil.AddUrl("/login/user/edit/head", control.EditUserHead, ginutil.POST)
	ginutil.AddUrl("/login/user/head", control.LookHead, ginutil.GET)
}

// #region @Api {title=新增}
// @param {name=data dataType=json paramType=body explain=LoginUser结构数据 required=true}
// @return {type=(bool, string, interface{}) explain=返回对象}
// @RequestMapping {name=Add type=POST value=/login/user/add}
// #endregion
func (control LoginUserController) Add(ctx *gin.Context) (bool, string, interface{}) {
	params := ginutil.GetParams(ctx)

	return LoginUserService{}.Add(ctx, params)
}

// #region @Api {title=修改当前用户密码}
// @param {name=oldPass dataType=string paramType=query explain=旧密码 required=true}
// @param {name=newPass dataType=string paramType=query explain=新密码 required=true}
// @param {name=owner dataType=string paramType=query explain=归属 required=true}
// @return {type=json explaintype=(bool, string, interface{})<int> explain=返回码值}
// @RequestMapping {name=EditPass type=POST value=/login/user/password/edit}
// #endregion
func (control LoginUserController) EditPass(ctx *gin.Context) (bool, string, interface{}) {
	loginUserId := moduleutil.CurrentLoginUserId(ctx) //取当前登录用户编号
	sOldPass := ginutil.GetParam(ctx, "oldPass", "").(string)
	sNewPass := ginutil.GetParam(ctx, "newPass", "").(string)

	bl, msg, da := sqlfactory.AnalysisEntity(moduleutil.GetMainDb(), "LoginUser")
	if !bl {
		return bl, msg, da
	}

	refer := da.(*dbinfo.TableInfo)

	return LoginUserService{}.EditPass(ctx, refer, loginUserId, sOldPass, sNewPass)
}

// 只有当前用户和管理员(admin)才能重置密码
// #region @Api {title=重置密码}
// @param {name=id dataType=string paramType=query explain=表编号 required=true}
// @param {name=pass dataType=int paramType=query explain=新密码 required=true}
// @return {type=json explaintype=(bool, string, interface{})<int> explain=返回码值}
// @RequestMapping {name=ResetPass type=POST value=/login/user/password/reset}
// #endregion
func (control LoginUserController) ResetPass(ctx *gin.Context) (bool, string, interface{}) {
	loginUserId := moduleutil.CurrentLoginUserId(ctx) //取当前登录用户编号
	id := ginutil.GetParam(ctx, "id", "").(string)
	pass := ginutil.GetParam(ctx, "pass", "").(string)

	bl, msg, da := sqlfactory.AnalysisEntity(moduleutil.GetMainDb(), "LoginUser")
	if !bl {
		return bl, msg, da
	}

	refer := da.(*dbinfo.TableInfo)

	return LoginUserService{}.ResetPass(ctx, refer, loginUserId, id, pass)
}

// #region @Api {title=根据用户编号设置用户类型}
// @param {name=id dataType=string paramType=query explain=用户编号 required=true}
// @param {name=type dataType=string paramType=query explain=用户类型 required=true}
// @return {type=json explaintype=(bool, string, interface{})<int> explain=返回码值}
// @RequestMapping {name=EditUserType type=POST value=/login/user/edit/type}
// #endregion
func (control LoginUserController) EditUserType(ctx *gin.Context) (bool, string, interface{}) {
	id := ginutil.GetParam(ctx, "id", "").(string)
	Type := ginutil.GetParam(ctx, "type", "").(string)

	bl, msg, da := sqlfactory.AnalysisEntity(moduleutil.GetMainDb(), "LoginUser")
	if !bl {
		return bl, msg, da
	}

	refer := da.(*dbinfo.TableInfo)

	return LoginUserService{}.EditUserType(ctx, refer, id, Type)
}

// #region @Api {title=设置用户头像}
// @param {name=head dataType=string paramType=query explain=Base64图片 required=true}
// @return {type=json explaintype=(bool, string, interface{})<int> explain=返回码值}
// @RequestMapping {name=EditUserHead type=POST value=/login/user/edit/head}
// #endregion
func (control LoginUserController) EditUserHead(ctx *gin.Context) (bool, string, interface{}) {
	loginUserId := moduleutil.CurrentLoginUserId(ctx) //取当前登录用户编号
	head := ginutil.GetParam(ctx, "head", "").(string)

	bl, msg, da := sqlfactory.AnalysisEntity(moduleutil.GetMainDb(), "LoginUser")
	if !bl {
		return bl, msg, da
	}

	refer := da.(*dbinfo.TableInfo)

	return LoginUserService{}.EditUserHead(ctx, refer, loginUserId, head)
}

// #region @Api {title=取用户头像}
// @return {type=bit explainType=image/png explain=返回图片流}
// @RequestMapping {name=LookHead type=GET value=/login/user/head}
// #endregion
func (control LoginUserController) LookHead(ctx *gin.Context) (bool, string, interface{}) {
	loginUserId := moduleutil.CurrentLoginUserId(ctx) //取当前登录用户编号

	bl, msg, res := LoginUserService{}.LookHead(loginUserId)
	if !bl {
		return bl, msg, res
	}

	// 设置HTTP响应头Content-Type为图片类型（这里是png）
	ctx.Header("Content-Type", "image/png")

	// 返回图片数据
	ctx.Data(200, "image/png", res.([]byte))

	return true, "图片下载", nil
}
